﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using GE.Font;

namespace GE
{
    class longroScreen : GameScreen
    {
        List<TimeText> textList = new List<TimeText>();
        public longroScreen()
        {
            textList.Add(new TimeText("어느 세상에 마왕이 살고 있었습니다.", 16, new Vector2(100, 100), Color.White,
                0.00f, 0.0f, 1.0f, 0.5f));
            textList.Add(new TimeText("마왕의 성에는 돈이고 공주고 아무것도 없었지만,", 16, new Vector2(100, 130), Color.White,
                0.00f, 0.5f, 1.5f, 0.5f));
            textList.Add(new TimeText("공명심에 눈이 먼 용사들은 이따금 마왕을 찾아와 귀찮게 굴고는 했습니다.", 16, new Vector2(100, 160), Color.White,
                0.00f, 1.0f, 2.0f, 0.5f));


        }

        double time = 0;
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            time += gameTime.ElapsedGameTime.TotalSeconds;

            if (time > 00.0 || Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                this.ExitScreen();
                ScreenManager.AddScreen(new GameplayScreen(), PlayerIndex.One);
                ScreenManager.AddScreen(new WaveScreen(0, null), PlayerIndex.One);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            // Our player and enemy are both actually just text strings.
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

            foreach(TimeText text in textList)
            {
                 text.Draw(spriteBatch, time);
            }

            spriteBatch.End();
        }
    }

    class TimeText
    {
        public float startTime, endTime, fadeTime, theta;
        public int size;
        public String text;
        public Color color;
        public Vector2 pos;

        public TimeText(String text, int size, Vector2 pos, Color color, float theta, 
            float startTime, float endTime, float fadeTime)
        {
            this.text = text;
            this.size = size;
            this.pos = pos;
            this.color = color;
            this.theta = theta;

            this.startTime = startTime;
            this.endTime = endTime;
            this.fadeTime = fadeTime;
        }

        public void Draw(SpriteBatch spriteBatch, double time)
        {
            if (time > startTime && time < endTime)
            {
                FreetypeFont.DrawString(spriteBatch, text, pos, theta, size, color);
            }
            else if (time < startTime && time > startTime - fadeTime)
            {
                FreetypeFont.DrawString(spriteBatch, text, pos, theta, size, 
                    new Color(color.R, color.G, color.B, (Byte)(color.A * (time-startTime)/(fadeTime))));
            }
            else if (time > endTime && time < endTime + fadeTime)
            {
                FreetypeFont.DrawString(spriteBatch, text, pos, theta, size,
                    new Color(color.R, color.G, color.B, (Byte)(color.A * (endTime - time) / (fadeTime))));
            }
        }
    }
}
